<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * 
 */

class PNC_Controller extends CI_Controller {
	const DAYOFF = 24;
	const UME = 'passerelles numeriques cambodia';
	const LEAVING = 'leave request for ume staff';
	const GATEWAY = 'A Gateway for Life';
	const EXT_MAIL = '@ume-kpc.edu.kh';
	const PASSWORD = '1234567';
	const EMP_CRUID = 'employee management';
	const LEAVE_CRUID = 'leaving request management';

	public function __construct() {
		parent::__construct();
		$this->load->model(array('mod_menu'));
	}

	/*
	 * View Main Page
	 * @access public
	 */

	public function view_main($data) {
		$this->load->view('master', $data);
	}

	/**
	 * @return bool
	 * @access public
	 */
	public function check_session() {
		if ($this->session->userdata('gm') || $this->session->userdata('submanager') || $this->session->userdata('admin') || $this->session->userdata('staff') || $this->session->userdata('manager')) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	/*
	 * @return void
	 * @access public
	 */

	public function check_access() {
		if (isset($_SERVER['HTTP_REFERER'])) {
			$this->session->set_userdata('previous_page', $_SERVER['HTTP_REFERER']);
		} else {
			$this->session->set_userdata('previous_page', base_url());
		}
	}

	/*
	 * Delete data checked from controller
	 * @access public
	 * @param string
	 * @param string
	 * @param string
	 * @param string
	 * @return void
	 */

	public function delete_checked($controller, $func, $mod, $checked_id, $lea_type, $where, $table) {
		if (!$checked_id) {
			$this->session->set_userdata('delete_error', show_message("Deleting was fail, please select check box!", "error"));
			if ($this->session->userdata('per_page')) {
				redirect($controller . '/' . $func . '/' . '?limit=' . $this->session->userdata('per_page'));
			}
			redirect($controller . '/' . $func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
		} else {
			$result = PNC_Model::delete_datas($where, $checked_id, $lea_type, $table); //give argument to delete_datas() in PNC_Model

			if ($result > 0) {
				$this->session->set_userdata('delete_checked', show_message("Deleted successfully", "success"));
				if ($this->session->userdata('per_page')) {
					redirect($controller . '/' . $func . '/' . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect($controller . '/' . $func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			} else {
				$this->session->set_userdata('delete_error', show_message("Deleting was fail, please select check box!", "error"));
				if ($this->session->userdata('per_page')) {
					redirect($controller . '/' . $func . '/' . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect($controller . '/' . $func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			}
		}
	}

	/* Send mail to manager */

	public function send_mail_request($con_success, $con_fail, $func_success, $func_fail) {
		if ($this->check_session()) {
			ini_set("SMTP", "smtp.gmail.com");
			$sender = $this->session->userdata('e_mail');
			$to = $this->session->userdata('reciever_mail');
			$subject = "Leaving Request";

			$link = base_url();
			$message = "Have leaving request from ";
			$message .= $this->session->userdata('e_mail') . " \n\n";
			$message .= "Dear,\n\n";
			$message .= "Reason: \n";
			$message .= $this->session->userdata('reason_leaving') . "\n\n";
			$message .= "Best regard, \n\n";
			$message .= "Please click " . $link . " to login wep application.";
			$headers = "From:" . $sender;
			$send = mail($to, $subject, $message, $headers);
			if ($send) {
//				$this->session->set_userdata('send_success', show_message('Did leaving request successfully!', 'success'));
				redirect($con_success . '/' . $func_success);
			} else {
//				$this->session->set_userdata('send_request_erro', show_message('Sending email for do request was failed 222!', 'error'));
				redirect($con_fail . '/' . $func_fail);
			}
		} else {
			redirect('main/');
		}
	}

	/* Send mail for reset password */
	/* Send mail to manager */

	public function send_reset_password($user, $email) {
		$password = random_string('alnum', 15);
		$this->db->where('staff_id', $user->staff_id);
		$this->db->update('pnc_employee', array('password' => MD5($password)));

		ini_set("SMTP", "smtp.gmail.com");
		$sender = 'sophea.sendmail@gmail.com';
		$to = $email;
		$subject = "Reset New Password";

		$link = base_url();
		$message = "This is in response to your request for login detailst at UME Leaving Request System \n \nLogin ID: " . $to;
		$message .= "\n\n Password: " . $password;
		$message .= "\n\n Thanks you!";
		$message .= "\n\n Admin Site";
		$headers = "From:" . $sender;
		$send = mail($to, $subject, $message, $headers);
		if ($send) {
			$this->session->set_userdata('send_reset_success', show_message('Please check your email for your new password!', 'success'));
			redirect(base_url());
		} else {
			$this->session->set_userdata('send_reset_error', show_message('Can not send mail please contact to your admin!', 'error'));
			redirect('main/reset_pass');
		}
	}

	/* Change status of record leaving request when onclick */

	public function approval_record($rcd_id, $current_status, $uri3, $controller, $own_func, $ask_func, $own_type, $ask_type, $disapprove) {
		if ($this->check_session()) {
			$result = $this->mod_gm->approve_update($rcd_id, $current_status, 'rcd_status', 'pnc_record');
			if ($result > 0) {
				if ($uri3 === $disapprove) {
					$this->session->set_userdata('success_disapprove', show_message('You have disapproved successfully!', 'success'));
				} else {
					$this->session->set_userdata('change_status_success', show_message('You have been approved successfully!', 'success'));
				}
			}
			if ($uri3 === $own_type) { /* Check for personal status */
				if ($this->session->userdata('per_page')) {
					redirect($controller . '/' . $own_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect($controller . '/' . $own_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			} else if ($uri3 === $ask_type OR $uri3 === $disapprove) { /* Check status of asking request */
				if ($this->session->userdata('per_page')) {
					redirect($controller . '/' . $ask_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect($controller . '/' . $ask_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			}
		} else {
			redirect('main/');
		}
	}

	/* Delete leaving request before approve */

	public function delete_before_approving($controller, $main_func) {
		if ($this->uri->segment(3) == 1) {
			$this->session->set_userdata('get_page', $this->uri->segment(3));
			$this->session->set_userdata('get_id', $this->uri->segment(4));
			$page = $this->session->userdata('get_page');
			$id = $this->session->userdata('get_id');
			$this->mod_record->delete_once($id, 'rcd_id', 'pnc_record', '');
			$this->session->set_userdata('delete_success', show_message('Deleted leaving request successfully!', 'success'));
			if ($this->session->userdata('per_page')) {
				redirect($controller . '/' . $main_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
			}
			redirect($controller . '/' . $main_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
		} else if ($this->uri->segment(3) == 2) {
			$this->session->set_userdata('page', $this->uri->segment(4));
			$pages = $this->session->userdata('page');
			$ts = $this->uri->total_segments(); /* Get last segment of uri */
			$desc_id = $this->uri->segment($ts);
			$id = $this->uri->segment($ts - 1);
			$this->mod_record->delete_once($id, 'rcd_id', 'pnc_record', $desc_id);
			$this->session->set_userdata('delete_success', show_message('Deleted leaving request successfully!', 'success'));
			if ($this->session->userdata('per_page')) {
				redirect($controller . '/' . $main_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
			}
			redirect($controller . '/' . $main_func . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
		}
	}

	/* Resize image when upload */

	public function resize_image($parth, $image, $new_width, $new_height) {
		$img_cfg_thumb['image_library'] = 'gd2'; //gd2 is the library image in ci
		$img_cfg_thumb['source_image'] = $parth . $image;
		$img_cfg_thumb['maintain_ratio'] = TRUE;
//        $img_cfg_thumb['new_image'] = $image;
		list($width, $height) = getimagesize($img_cfg_thumb['source_image']);
		if ($parth == './uploads/users/') {
			$img_cfg_thumb['width'] = $new_width;
			$img_cfg_thumb['height'] = $new_height;
		} else {
			$img_cfg_thumb['width'] = $new_width;
			$img_cfg_thumb['height'] = ($height * $new_width) / $width;
		}
		$this->load->library('image_lib');
		$this->image_lib->initialize($img_cfg_thumb);
		$this->image_lib->resize();
	}

}

/* End of file PNC_Controller.php */
/* Location: ./application/core/PNC_Controller.php */